import { defineStore } from 'pinia'
import { ref } from 'vue'
import { getLocalToken,removeLocalToken,setLocalToken } from '@/utils/auth'
import type { Profile } from '@/types/user'
export const useUserStore = defineStore('hm-user', () => {
  //token信息
  const token = ref(getLocalToken() || '')
    // 个人信息
  const profile = ref<Profile>({} as Profile)
  const setProfile = (p: Profile) => {
      profile.value = p
  }
  //登陆成功 保存token
  const setToken = (val:string) => {
    token.value= val
    setLocalToken(val)
  }

  //退出登录 删除Token
  const cleanToken = () => {
    token.value = ''
    removeLocalToken()

  }
  // 清空用户信息
  const clearProfile = () => {
    profile.value = {} as Profile
  }
  //记得返回
  return {
    token,
    setToken,
    cleanToken,
    profile,
    setProfile,
    clearProfile
  }
})
